package Interview;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by Administrator on 2017/4/1.
 */
class Node {
    Node left = null;
    Node right = null;
    int value;
}

public class _qunawang_20170401Test1 {



    public static Node buildTree(int[] preOrder, int start,
                                 int[] inOrder, int end, int length) {
        //参数验证
        if (preOrder == null || preOrder.length == 0 || inOrder == null
                || inOrder.length == 0 || length <= 0) {
            return null;
        }

        int value = preOrder[start];
        Node root = new Node();
        root.value = value;
        if (length == 1)
            return root;
        int i = 0;
        while (i < length) {
            if (value == inOrder[end - i]) {
                break;
            }
            i++;
        }
        root.left = buildTree(preOrder, start + 1, inOrder, end - i - 1, length - 1 - i);
        root.right = buildTree(preOrder, start + length - i, inOrder, end, i );

        return root;
    }

    public static void levelOrderTraversal(Node root, List<Integer> out){
        if(root==null){
            return;
        }
        ArrayDeque<Node> queue=new ArrayDeque<Node>();
        queue.add(root);
        while(queue.isEmpty()==false){
            Node node=queue.remove();
            out.add(node.value);
            if(node.left!=null){
                queue.add(node.left);
            }
            if(node.right!=null){
                queue.add(node.right);
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int num = in.nextInt();
            int[] qian = new int[num];
            for (int i = 0; i < num; i++) {
                qian[i] = in.nextInt();
            }
            int [] zhong = new int [num];
            for (int i = 0; i < num; i++) {
                zhong[i] = in.nextInt();
            }
            Node root = buildTree(qian, 0, zhong, zhong.length - 1, zhong.length);
            List<Integer> out = new ArrayList<>();
            levelOrderTraversal(root,out);
            for (int i = 0; i < out.size()-1; i++) {
                System.out.println(out.get(i)+" ");
            }
            System.out.println(out.get(out.size()-1));
        }

    }

}
